<template>
  <view class="visit-container">
    <view class="visit-header">
      <view class="visit-search-input-box">
        <view class="visit-search-input-icon">
          <image src="/static/visit/search.png"></image>
        </view>
        <input
          class="visit-search-input"
          type="text"
          placeholder="请输入患者姓名/身份证号/手机号"
          v-model="keyword"
          @confirm="search"
          confirm-type="search"
        />
      </view>
      <view class="visit-search-button">
        <button type="primary" @tap="search" class="visit-button">搜索随访记录</button>
      </view>
      <view class="visit-search-text">
        <text class="visit-search-text-title">草稿数：</text>
        <text class="visit-search-text-num">{{
          visitVO == undefined ? 0 : visitVO?.draftNum
        }}</text>
      </view>
      <view class="visit-search-text">
        <text class="visit-search-text-title">正式随访数：</text>
        <text class="visit-search-text-num">{{
          visitVO == undefined ? 0 : visitVO?.completedNum
        }}</text>
      </view>
      <view class="visit-search-fliter" @tap="showF">
        <image src="/static/visit/fliter.png"></image>
        <text>日期筛选</text>
      </view>
    </view>
    <view class="visit-search-fliter-box" v-if="showFliter">
      <view
        :class="dateType == 0 ? 'visit-search-fliter-item-active' : 'visit-search-fliter-item'"
        @tap="selectDate(0)"
      >
        全部时间
      </view>
      <view
        :class="dateType == 3 ? 'visit-search-fliter-item-active' : 'visit-search-fliter-item'"
        @tap="selectDate(3)"
      >
        最近三个月
      </view>
      <view
        :class="dateType == 6 ? 'visit-search-fliter-item-active' : 'visit-search-fliter-item'"
        @tap="selectDate(6)"
      >
        最近半年</view
      >
      <view
        :class="dateType == 12 ? 'visit-search-fliter-item-active' : 'visit-search-fliter-item'"
        @tap="selectDate(12)"
      >
        最近一年</view
      >
    </view>
    <view class="visit-body">
      <scroll-view class="visit-list" @scrolltolower="lowerBottom" scroll-y :show-scrollbar="false">
        <view
          class="visit-item"
          v-for="(visitItem, index) in visitVO?.visitVOList"
          :key="index"
          @tap="toVisitDesc(visitItem)"
        >
          <view class="visit-ribbon visit-due" v-if="visitItem.visitSign == 2">
            <text>已逾期</text>
          </view>
          <view class="visit-ribbon" v-if="visitItem.visitSign == 1">
            <text>已完成</text>
          </view>
          <view class="visit-ribbon visit-save" v-if="visitItem.visitSign == 0">
            <text>存草稿</text>
          </view>
          <view class="visit-ribbon visit-yuyue" v-if="visitItem.visitSign == -1">
            <text>已预约</text>
          </view>
          <view class="visit-item-text">
            <text>{{ visitItem.name }}</text>
          </view>
          <view class="visit-item-text">
            <text>{{ visitItem.sex }}</text>
          </view>
          <view class="visit-item-text">
            <text>{{ visitItem.age }}岁</text>
          </view>
          <view class="visit-item-text">
            <text>{{ visitItem.disease }}</text>
          </view>
          <view class="visit-item-text">
            <text>{{ visitItem.visitTime }}</text>
          </view>
        </view>
      </scroll-view>
    </view>
    <view class="addVisit-button" @tap="toSearchVisit">
      <view class="addVisit-button-inner">新增随访</view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { http } from '@/utils/http'
import { useMemberStore } from '@/stores/modules/member'
import { useVisitStore } from '@/stores/modules/visit'
interface Visit {
  draftNum: number //草稿数
  completedNum: number //正式随访数
  visitVOList: Array<VisitItem> //随访列表
}
interface VisitItem {
  id: number //随访id
  name: string //患者姓名
  sex: string //患者性别
  age: string //患者年龄
  disease: string //患者疾病
  doctorId: number //医生id
  patientId: number //患者id
  checkId: number //体检信息id
  symptoms: string //随访患者症状
  drugUsage: string //用药情况
  visitTime: string //随访时间
  visitWay: string //随访方式
  livingHabit: string //生活习惯
  visitSign: number //是否完成随访，0代表草稿，1代表未完成随访，2代表完成随访
  remark: string //备注
}
const visitVO = ref<Visit>()
//无筛选
const page = ref(1)
//flag
const getType = ref(1)
//日期筛选类型
const dateType = ref(0)
//是否显示日期筛选
const showFliter = ref(false)
const pageSize = ref(8)
const keyword = ref('')
const doctorStore = useMemberStore()
const visitStore = useVisitStore()

//获取随访列表（无筛选）
const getVisitVo = () => {
  http<Visit>({
    url:
      '/visit/getVisitList?doctorId=' +
      doctorStore.profile.doctorInfo.id +
      '&page=' +
      page.value +
      '&pageSize=' +
      pageSize.value,
    method: 'GET',
  }).then((res: any) => {
    if (page.value == 1) {
      visitVO.value = res.data
    } else {
      visitVO.value!.draftNum = res.data.draftNum
      visitVO.value!.completedNum = res.data.completedNum
      visitVO.value!.visitVOList = [...visitVO.value!.visitVOList, ...res.data.visitVOList]
    }
    console.log(visitVO.value)
  })
}

//搜索随访列表
const search = () => {
  console.log(keyword.value)
  http<Visit>({
    url:
      '/visit/searchVisitByKeyword?keyword=' +
      keyword.value +
      '&doctorId=' +
      doctorStore.profile.doctorInfo.id,
    method: 'GET',
  }).then((res: any) => {
    visitVO.value = res.data
    console.log(visitVO.value)
  })
}

//通过日期筛选随访列表
const getVisitByDate = () => {
  http<Visit>({
    url:
      '/visit/flierVisitByDate?doctorId=' +
      doctorStore.profile.doctorInfo.id +
      '&type=' +
      dateType.value,
    method: 'GET',
  }).then((res: any) => {
    visitVO.value = res.data
    console.log(visitVO.value)
  })
}

//滑到底部触发（下拉刷新）
const lowerBottom = () => {
  if (getType.value == 1) {
    page.value = page.value + 1
    getVisitVo()
  }
}

//改变筛选框显示状态
const showF = () => {
  showFliter.value = !showFliter.value
}

//改变日期筛选
const selectDate = (num: number) => {
  keyword.value = ''
  dateType.value = num
  if (dateType.value == 0) {
    page.value = 1
    getVisitVo()
  } else {
    getVisitByDate()
  }
}

//跳转到搜索页
const toSearchVisit = () => {
  uni.navigateTo({ url: '/pages/index/visit/components/SearchVisit' })
}

//跳转到随访详情页
const toVisitDesc = (visitItem: VisitItem) => {
  visitStore.setVisit(visitItem)
  uni.navigateTo({ url: '/pages/index/visit/components/VisitDesc' })
}

onLoad(() => {
  getVisitVo()
})

onShow(() => {
  page.value = 1
  getVisitVo()
})
</script>

<style scoped lang="scss">
.visit-container {
  padding: 10rpx 5rpx 0 5rpx;
  position: relative;
}
.visit-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15rpx;
  padding: 0 10rpx;
  width: 100%;
  height: 45rpx;
  background-color: #fff;
  .visit-search-input-box {
    display: flex;
    align-items: center;
    height: 32rpx;
    width: 250rpx;
    padding: 0 20rpx;
    background-color: #f5f5f5;
    border-radius: 5rpx;
    .visit-search-input-icon {
      width: 15rpx;
      height: 15rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 10rpx;
    }
    .visit-search-input {
      flex: 1;
      border: none;
      outline: none;
      background-color: transparent;
      width: 100%;
      height: 100%;
    }
    image {
      width: 13rpx;
      height: 13rpx;
    }
  }
  .visit-search-text {
    font-size: 12rpx;
    .visit-search-text-title {
      color: #61666d;
    }
    .visit-search-text-num {
      color: #333333;
    }
  }
  .visit-search-fliter {
    display: flex;
    justify-content: space-around;
    align-items: center;
    margin-left: 5rpx;
    image {
      width: 18rpx;
      height: 18rpx;
      margin-right: 6rpx;
    }
    text {
      font-size: 13rpx;
      color: #333333;
    }
  }
}
.visit-search-fliter-box {
  position: absolute;
  top: 45rpx;
  right: 10rpx;
  width: 100rpx;
  height: 105rpx;
  border-radius: 10rpx;
  background-color: #f5f5f5;
  z-index: 1;
  text-align: center;
  padding: 5rpx 0;
  .visit-search-fliter-item {
    font-size: 12rpx;
    padding: 4rpx;
    color: #333333;
    border-radius: 8rpx;
  }
}
.visit-search-fliter-item-active {
  color: #fff;
  background-color: #06d9f5;
  background: #fff radial-gradient(#06dbf5, #02abf3);
  font-size: 12rpx;
  padding: 4rpx;
  border-radius: 8rpx;
}

.visit-body {
  width: 100%;
  height: calc(100vh - 120rpx);
  margin-top: 10rpx;
  overflow: auto;
  .visit-list {
    width: 100%;
    .visit-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #fff;
      position: relative;
      overflow: hidden;
      height: 50rpx;
      padding: 0 10rpx;
      margin-bottom: 4rpx;
      .visit-ribbon {
        background-color: #1890ff;
        overflow: hidden;
        white-space: nowrap;
        position: absolute;
        left: -8rpx; // 根据实际调整即可
        top: 8rpx; // 根据实际调整即可
        transform: rotate(-45deg);
        box-shadow: 0 0 1rpx #888;
        text {
          color: #fff;
          padding: 0rpx 10rpx;
          display: block;
          font-size: 8rpx;
        }
      }
      .visit-due {
        background-color: red;
      }
      .visit-save {
        background-color: #61666d;
      }
      .visit-yuyue {
        background-color: #f39e0a;
      }
    }
    .visit-item-text {
      width: 20%;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: hidden;
      font-size: 12rpx;
      color: #333333;
      text-align: center;
    }
  }
}
.addVisit-button {
  position: absolute;
  right: 35rpx;
  bottom: 30rpx;
  width: 45rpx;
  height: 45rpx;
  border-radius: 50%;
  background-color: #06d9f5;
  background: #fff radial-gradient(#06dbf5, #02abf3);
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  .addVisit-button-inner {
    width: 32rpx;
    height: 32rpx;
    font-size: 12rpx;
    color: #fff;
  }
}

.visit-button {
  background-color: #06d9f5;
  background: #fff radial-gradient(#06dbf5, #02abf3);
}
</style>
